﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace AcfunUpload
{
    public static class EncodingHelper
    {  
        /// <summary>
       /// 取Unix时间戳（13位）
       /// </summary>
       /// <returns>Unix时间戳</returns>
        public static long timeStamp()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }
        /// <summary>
        /// UTF-8编码
        /// </summary>
        /// <param name="value">原始数据</param>
        /// <returns></returns>
        public static string encodeutf8(string value)
        {

            return HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(value));
        }
        /// <summary>
        /// UTF-8解码
        /// </summary>
        /// <param name="value">编码数据</param>
        /// <returns></returns>
        public static string decodeutf8(string value)
        {
            return HttpUtility.UrlDecode(value, Encoding.UTF8);
        }
        /// <summary>
        /// GBK编码
        /// </summary>
        /// <param name="value">原始数据</param>
        /// <returns></returns>
        public static string encodegbk(string value)
        {
            return HttpUtility.UrlEncode(value, Encoding.GetEncoding("GBK"));
        }
        /// <summary>
        /// GBK解码
        /// </summary>
        /// <param name="value">编码数据</param>
        /// <returns></returns>
        public static string decodegbk(string value)
        {
            return HttpUtility.UrlDecode(value, Encoding.GetEncoding("GBK"));
        }
        /// <summary>
        /// Unicode解码（也就是usc2到ansi）
        /// </summary>
        /// <param name="value">编码数据（/u554A/u554A/u662F/u5927）</param>
        /// <returns></returns>
        public static string decodeUnicode(string value)
        {
            if (value != null)
            {
                StringBuilder sb = new StringBuilder();
                string pattern = @"\\u[0-9a-fA-F]{4}";
                var matches = Regex.Matches(value, pattern);
                foreach (Match match in matches)
                {
                    sb.Append(char.ConvertFromUtf32(Int32.Parse(match.Value.Substring(2, 4), System.Globalization.NumberStyles.HexNumber)));
                }
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="value">编码数据</param>
        /// <returns></returns>
        public static string decodeBase64(string value)
        {
            byte[] bpath = Convert.FromBase64String(value);
            return ASCIIEncoding.Default.GetString(bpath);
        }
        /// <summary>
        /// base64编码
        /// </summary>
        /// <param name="value">原始数据</param>
        /// <returns></returns>
        public static string encodeBase64(string value)
        {
            Encoding encode = Encoding.ASCII;
            byte[] bytedata = encode.GetBytes(value);
            return Convert.ToBase64String(bytedata, 0, bytedata.Length);
        }
    }
}
